package com.hn.exception;

import com.hn.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 */
//@ControllerAdvice(annotations = {RestController.class})//加了RestController注解的出异常会进入这里
@RestControllerAdvice
@Slf4j
public class BusinessException {
    @ExceptionHandler(Exception.class)
    public Result excepionHandler(Exception ex){
        //解决名字重复问题
        if(ex.getMessage().contains("Duplicate entry")){
            String[] s=ex.getMessage().split(" ");
            return Result.error(s[2]+"已存在");
        }
        log.info("异常出现："+ex.getMessage());
        return Result.error("未知错误");
    }
}
